package test;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class TextAnnimation extends JPanel{
	/**
	 * 
	 */
	private static final long serialVersionUID = 4927366799736786908L;
	private int x;
	private int y;
	private String str;
	
	public TextAnnimation()
	{
		this.x = -50;
		this.y = 150;
		this.str = "Display string";
		setSize(400,400);
	}
	
	private void start() throws InterruptedException
	{
		while(true)
		{
			while(x<=(getWidth()-100))
			{
				x++;
				y = getHeight()/2;
				repaint();
				Thread.sleep(10);
			}
		
			while(x >=0)
			{
				x--;
				y = getHeight()/2;
				repaint();
				Thread.sleep(10);
			}
		}
	}
	
	@Override
	public void paint(Graphics g) {
		g.setColor(Color.cyan);
		g.fillRect(0, 0, 400, 400);
		g.setColor(Color.RED);
		Font font = new Font("Serif",Font.BOLD,50);
		g.setFont(font);
		g.drawString(str, x, y);
		
		g.setColor(Color.GREEN);
		g.fillOval(x, y+40, 50, 50);
		g.fillOval(x, y-100, 50, 50);
	}
	
	public static void main(String[] args)
	{
		TextAnnimation ta = new TextAnnimation();
		JFrame jf = new JFrame();
		jf.setSize(400, 400);
		jf.add(ta);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf.setVisible(true);
		try {
			ta.start();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}

